package com.mx102.bandyta;

import java.util.Timer;
import java.util.TimerTask;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Bandyta implements ApplicationListener, InputProcessor {

	private static final int WIDTH = 640;
	private static final int HEIGHT = 480;
	
	private float skalaX,skalaY;

	private Camera kamera;
	private SpriteBatch sb;

	private AssetManager manager;
	private boolean zaladowany;
	private BitmapFont napis;

	private Beben beben;
	private Tlo tlo;
	private Kredyty kredyty;

	private Timer timer;
	

	private void zakonczLadowanie() {
		
		beben = new Beben(manager);
		tlo = new Tlo(manager);
		kredyty = new Kredyty(manager);
		zaladowany=true;
	}
	
	@Override
	public void create() {

		// Ladowanie obrazow i dzwiekow
		manager = new AssetManager();
		manager.load("background.png", Texture.class);
		manager.load("cyfry.png", Texture.class);
		manager.load("kreciolekl.png", Texture.class);
		manager.load("kreciolekp.png", Texture.class);
		manager.load("ogienl.png", Texture.class);
		manager.load("ogienp.png", Texture.class);
		manager.load("symbole.png", Texture.class);
		manager.load("walec_1.png", Texture.class);
		manager.load("walec_2.png", Texture.class);
		manager.load("walec_3.png", Texture.class);
		manager.load("play.wav",Sound.class);
		manager.load("spin.wav",Music.class);
		manager.load("stop1.wav",Sound.class);
		manager.load("stop2.wav",Sound.class);
		manager.load("stop3.wav",Sound.class);
		
		zaladowany=false;

		// Timer konczacy losowanie
		timer = new Timer();

		// Kamera przeskalowujaca obraz do rozdzielczosci urzadzenia
		kamera = new OrthographicCamera(WIDTH, HEIGHT);
		kamera.translate(WIDTH / 2, HEIGHT / 2, 0);
		sb = new SpriteBatch();

		napis = new BitmapFont();

		Gdx.input.setInputProcessor(this);

	}

	@Override
	public void dispose() {
		System.exit(0);
	}

	@Override
	public void pause() {
	

	}

	@Override
	public void render() {

		// Inicjalizacja grafiki
		float delta = Gdx.graphics.getDeltaTime();
		kamera.update();
		kamera.apply(Gdx.gl10);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		sb.setProjectionMatrix(kamera.combined);
		sb.begin();
		

		// Wyswietlenie grafiki
		if(zaladowany){
			sb.disableBlending();
			tlo.render(sb, delta);
			kredyty.render(sb, delta);
			beben.render(sb, delta);
			sb.enableBlending();
		} 
		
		// Preloader
		manager.update();
		if (manager.getProgress() < 1) {
			napis.setColor(1.0f, 1.0f, 1.0f, 1.0f);
			napis.setScale(5);
			String postep = Float.toString(manager.getProgress() * 100).split("\\.")[0]+ "%";
			napis.draw(sb, postep, 320 - napis.getBounds(postep).width / 2, 240);
		} else if (!zaladowany){
			zakonczLadowanie();
		}

		// Koniec generowania grafiki
		sb.end();

	}

	@Override
	public void resize(int width, int height) {
		
		// Wyznaczenie skalowania prze zmianie rozdzielczosci
		
		this.skalaX=(float)width/WIDTH;
		this.skalaY=(float)height/HEIGHT;

	}

	@Override
	public void resume() {

	}

	@Override
	public boolean keyDown(int arg0) {
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int arg2, int arg3) {

		// Przeskalowanie
		float xs=x/skalaX;
		float ys=y/skalaY;

		// Poczatek losowania
		if (!beben.losuje() && (xs > 100) && (xs < 540) && (ys > 133) && (ys < 388)) {

			beben.start();
			kredyty.zwiekszKredyt();

			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					beben.stop();
				}
			}, 2000);

		}
		return false;
	}

}
